<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 操作日志
 */
declare (strict_types=1);

namespace app\backend\controller;

use app\backend\base\controller\Base;
use app\backend\validate\OperateLogValidate;

class Operatelog extends Base
{
    /**
     * 列表
     */
    public function index()
    {
        if(request()->isPost()){
            $validate = new OperateLogValidate();
            $ret = $validate->scene('index')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $page = input('page/d',1);
            $limit = input('limit/d',20);
            $fieldName = input('field_name','');
            $fieldValue = input('field_value','');
            $dateType = input('date_type','created_at');
            $startDate = input('start_date','');
            $endDate = input('end_date','');

            $where = [
                [$dateType,'between',[$startDate,$endDate]]
            ];

            //字段筛选
            if (!empty($fieldName) && !empty($fieldValue)){
                switch($fieldName){
                    case 'nickname'://昵称
                        $where[] = ['nickname','like',"%{$fieldValue}%"];
                        break;
                }
            }

            $list = [];
            $count = \app\extend\model\OperateLog::where($where)->count();
            if($count > 0){
                $list = \app\extend\model\OperateLog::where($where)->append(['admin_account_txt'])->order(['id'=>'desc'])->page($page)->limit($limit)->select();
            }

            return json([
                'code' => 0,
                'msg' => '获取成功',
                'count' => $count,
                'data' => $list,
            ])->send();
        } else {
            return view('index');
        }
    }
}
